import { bus } from "../../network/index"

// pages/category/category.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    sudokuList: [],
    shopsList: []
  },
  search(event) {
    wx.navigateTo({
      url: '/pages/search/search?val=' + event.detail,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSudoku();
  },
  select(event) {
    let i = event.currentTarget.dataset.key;
    app.globalData.selectIndex = i;
    this.setIndex(i)
  },
  getShops(i) {
    let id = this.data.sudokuList[i].id
    // console.log(id)
    bus.goodsList({ categoryId: id }).then((res) => {
      console.log(res.data);
      if (res.data.code === 0) {
        this.setData({
          shopsList: res.data.data
        })
      } else {
        this.setData({
          shopsList: []
        })
      }
    }).catch((err) => {
      console.log(err);
    })
  },
  getSudoku() {
    bus.category().then((res) => {
      let data = res.data.data;
      this.setData({
        sudokuList: data
      })
      // console.log(this.data.sudokuList);
      this.getShops(this.data.activeKey)
    }).catch((err) => {
      console.log(err);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  setIndex(i) {
    if (i !== this.data.activeKey) {
      this.setData({
        activeKey: i
      })
      this.getShops(i)
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let i = app.globalData.selectIndex;
    this.setIndex(i);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})